import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class SolutionLCP52 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    static class Segment{
        int start;
        int end;
        int total;
        int color;
        Segment left;
        Segment right;

        public Segment(int s,int e,int c){
            start=s;
            end=e;
            color=c;
            if (c==1){
                total=end-start+1;
            }
        }

        public void update(int l,int r,int c){
            if (l>end || r<start){
                return;
            }
            if (l<=start && end<=r){
                color=c;
                if (c==1){
                    total=end-start+1;
                }else{
                    total=0;
                }
                left=right=null;
                return;
            }
            int mid=(start+end)/2;
            if (left==null){
                left=new Segment(start,mid,color);
            }
            if (right==null){
                right=new Segment(mid+1,end,color);
            }
            left.update(l,r,c);
            right.update(l,r,c);
            total=left.total+ right.total;
        }
    }

    public int getNumber(TreeNode root, int[][] ops) {
        TreeSet<Integer> set=new TreeSet<>();
        dfs(root,set);
        Map<Integer,Integer> map=new HashMap<>();
        int id=0;
        for (Integer integer : set) {
            map.put(integer,id);
            id++;
        }
        Segment segment=new Segment(0,id-1,0);
        for (int[] op : ops) {
            Integer a=set.floor(op[1]);
            Integer b=set.ceiling(op[2]);
            if (a!=null && b!=null){
                segment.update(map.get(a),map.get(b),op[0]);
            }
        }
        return segment.total;
    }

    private void dfs(TreeNode root, Set<Integer> list) {
        if (root==null){
            return;
        }
        dfs(root.left,list);
        list.add(root.val);
        dfs(root.right,list);
    }

    public static void main(String[] args) {
        System.out.println(new SolutionLCP52().getNumber(new TreeNode(1,null,new TreeNode(2,null,new TreeNode(3,null,new TreeNode(4,null,new TreeNode(5))))),new int[][]{{1,2,4},{1,1,3},{0,3,5}}));
    }
}
